package cn.pigeon.health.controller;

import cn.pigeon.health.constant.MessageConstant;
import cn.pigeon.health.constant.RedisKeyConstant;
import cn.pigeon.health.entity.Result;
import cn.pigeon.health.pojo.Member;
import cn.pigeon.health.pojo.Order;
import cn.pigeon.health.pojo.OrderSetting;
import cn.pigeon.health.service.MemberService;
import cn.pigeon.health.service.OrderService;
import cn.pigeon.health.service.OrderSettingService;
import cn.pigeon.health.utils.DateUtils_Extend;
import cn.pigeon.health.utils.ValidateCodeUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/orderInfo")
public class OrderInfoController {
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private OrderService orderService;

    /**
     * 发送短信验证码
     * @param telephone
     * @return
     */
    @RequestMapping("/sendValidateCode")
    public Result sendValidateCode(String telephone){
        //产生4位随机验证码
        String vCode = ValidateCodeUtils.generateValidateCode(4).toString();
        try {
            //取消短信发送，默认成功,省钱啊
            //SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,vCode);
            //打印验证码码到控制台，方便前端填写验证码
            System.out.println("当前发送的验证码码是:"+vCode+"，时间:"+
                    DateUtils_Extend.parseDate2String(new Date(),"yyyy-MM-dd HH:mm:ss"));
            //保存验证码到Redis,便于后续验证   手机号+一个自定义常量 做key
            jedisPool.getResource().setex(telephone+ RedisKeyConstant.SENDTYPE_ORDER,5*60,vCode);
            return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }

    /**
     * 提交预约信息
     * @param map
     * @return
     */
    @RequestMapping("/submitOrder")
    public Result submitOrder(@RequestBody Map map){
        try {
            //手机号
            String telephone = (String) map.get("telephone");
            //验证码
            String vCode = (String) map.get("validateCode");

            //1.判断验证码是否输入正确   和Redis缓存中的验证码进行对比
            String vCode_Redis = jedisPool.getResource().get(telephone + RedisKeyConstant.SENDTYPE_ORDER);
            if(vCode_Redis==null || vCode==null || !vCode_Redis.equals(vCode)){
                return new Result(false,MessageConstant.VALIDATECODE_ERROR);
            }

            //订单类型
            map.put("orderType",Order.ORDERTYPE_WEIXIN);

            return orderService.makeOrder(map);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据预约Id查询预约详情
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Map map = orderService.findById(id);
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",
                    DateUtils_Extend.parseDate2String(orderDate,"yyyy-MM-dd"));
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
